package com.yeyks.commonReference.service.hotel.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yeyks.common.constants.ConfigKey;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.hotel.HotelSearchB2cService;
import com.yeyks.commonReference.service.hotel.HotelSearchUtilService;
import com.yeyks.commonReference.utils.ConfidentialOperationUtil;
import com.yeyks.hotel.dal.dao.HotelInfoMapper;
import com.yeyks.hotel.dal.dao.RoomCalendarInfoMapper;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.dal.domain.RoomCalendarInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.param.HotelInfoB2cHotelSearchParam;
import com.yeyks.hotel.param.HotelInfoHotelSearchOrderHotelInfoParam;
import com.yeyks.hotel.utils.HotelSearchUtil;
import com.yeyks.hotel.vo.HotelInfoHotelSearchOrderHotelInfoVo;
import com.yeyks.hotel.vo.HotelInfoHotelSearchVo;
import com.yeyks.hotel.vo.HotelInfoHotelSearchVoHotle;
import com.yeyks.hotel.vo.HotelInfoHotelSearchVoRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class HotelSearchB2cServiceImpl implements HotelSearchB2cService {

    @Autowired
    private HotelInfoMapper hotelInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomCalendarInfoMapper roomCalendarInfoMapper;
    @Autowired
    private ConfigService configService;
    @Autowired
    private HotelSearchUtilService hotelSearchUtilService;

    /**
     * v3.4
     * b2c酒店搜索
     */
    @Override
    public HotelInfoHotelSearchVo b2cHotelSearch(@Validated HotelInfoB2cHotelSearchParam param) {
        //修正时间
        param.setStartDate(DateUtil.beginOfDay(param.getStartDate()));
        param.setEndDate(DateUtil.beginOfDay(param.getEndDate()));
//        //时间修正
//        param.setStartDate(HotelSearchUtil.fixDate(param.getStartDate()));
//        param.setEndDate(HotelSearchUtil.fixDate(param.getEndDate()));
        //时间校验
        HotelSearchUtil.checkSearchDate(param.getStartDate(), param.getEndDate());
        //酒店列表
        List<HotelInfoHotelSearchVoHotle> hotelInfoB2cHotelSearchVos = null;
        com.github.pagehelper.PageInfo<HotelInfoHotelSearchVoHotle> hotelInfoB2cHotelSearchVoPageInfo = null;
        int count = 3;
        Integer distance = param.getDistance();
        //获取设置的酒店数量下限
        Integer minCount = Convert.toInt(configService.getValue(ConfigKey.HOTEL_SEARCH_B2C_HOTEL_COUNT), 15);
        while ((count-- > 0) && (null == hotelInfoB2cHotelSearchVoPageInfo || hotelInfoB2cHotelSearchVoPageInfo.getTotal() < minCount)) {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            hotelInfoB2cHotelSearchVos = hotelInfoMapper.b2cHotelSearchHotels(param.getPositionX(), param.getPositionY(), param.getHotelName(), distance, param.getSort());
            hotelInfoB2cHotelSearchVoPageInfo = new com.github.pagehelper.PageInfo<>(hotelInfoB2cHotelSearchVos);
            distance *= 2;
        }
        List<HotelInfoHotelSearchVoHotle> b2cHotelSearchVoPageInfoList = new ArrayList<>();
        if (hotelInfoB2cHotelSearchVoPageInfo.getSize() > 0) {
            b2cHotelSearchVoPageInfoList = hotelInfoB2cHotelSearchVoPageInfo.getList();
            Iterator<HotelInfoHotelSearchVoHotle> iterator = b2cHotelSearchVoPageInfoList.iterator();
            while (iterator.hasNext()){
                HotelInfoHotelSearchVoHotle hotelInfoHotelSearchVoHotle = iterator.next();
                //房间列表
                List<RoomInfo> roomInfos = roomInfoMapper.selectList(
                        new QueryWrapper<RoomInfo>().in(RoomInfo.ID, hotelInfoHotelSearchVoHotle.getRoomIds()));
                ArrayList<HotelInfoHotelSearchVoRoom> hotelInfoB2cHotelSearchVoRooms = new ArrayList<>();
                for (RoomInfo roomInfo : roomInfos) {
                    //去除房间多余信息
                    ConfidentialOperationUtil.dealRoomInfo(roomInfo);
                    hotelInfoB2cHotelSearchVoRooms.add(BeanUtil.toBean(roomInfo, HotelInfoHotelSearchVoRoom.class));
                }
                hotelInfoHotelSearchVoHotle.setRooms(hotelInfoB2cHotelSearchVoRooms);
                //房态
                hotelSearchUtilService.perfectHotelInfoB2cHotelSearchVoRoomsAttribute(hotelInfoHotelSearchVoHotle, param.getStartDate(), param.getEndDate());
                if(CollectionUtil.isEmpty(hotelInfoHotelSearchVoHotle.getRooms())){
                    iterator.remove();
                }
            }
        }
        HotelInfoHotelSearchVo hotelInfoB2cHotelSearchVo = new HotelInfoHotelSearchVo().setList(b2cHotelSearchVoPageInfoList);
        return hotelInfoB2cHotelSearchVo;
    }

    //C2B订单房态信息:v3.4
    @Override
    public HotelInfoHotelSearchOrderHotelInfoVo b2cHotelOrderCalendar(HotelInfoHotelSearchOrderHotelInfoParam param) {
        return getHotelInfoHotelSearchOrderHotelInfoVo(param);
    }

    private HotelInfoHotelSearchOrderHotelInfoVo getHotelInfoHotelSearchOrderHotelInfoVo(HotelInfoHotelSearchOrderHotelInfoParam param) {
        //修正时间
        param.setStartDate(DateUtil.beginOfDay(param.getStartDate()));
        param.setEndDate(DateUtil.beginOfDay(param.getEndDate()));
//        //时间修正
//        param.setStartDate(HotelSearchUtil.fixDate(param.getStartDate()));
//        param.setEndDate(HotelSearchUtil.fixDate(param.getEndDate()));
        //时间校验
        HotelSearchUtil.checkSearchDate(param.getStartDate(), param.getEndDate());
        //查询
        RoomInfo roomInfo = roomInfoMapper.selectById(param.getRoomId());
        //去除信息
        ConfidentialOperationUtil.dealRoomInfo(roomInfo);
        HotelInfo hotelInfo = hotelInfoMapper.selectById(roomInfo.getHotelId());
        List<RoomCalendarInfo> roomCalendarInfos = roomCalendarInfoMapper.selectList(new QueryWrapper<RoomCalendarInfo>()
                .eq(RoomCalendarInfo.ROOM_ID, param.getRoomId())
                .ge(RoomCalendarInfo.DATE, param.getStartDate())
                .lt(RoomCalendarInfo.DATE, param.getEndDate()));
        if (null == roomCalendarInfos || roomCalendarInfos.size() == 0) {
            throw new ServiceException("房态信息不存在");
        }
        for (RoomCalendarInfo roomCalendarInfo : roomCalendarInfos) {
            //去除信息
            ConfidentialOperationUtil.dealRoomCalendarInfo(roomCalendarInfo);
        }
        //设值
        HotelInfoHotelSearchOrderHotelInfoVo c2bSelectRoomCalendarInfoForOrderVo = new HotelInfoHotelSearchOrderHotelInfoVo();
        c2bSelectRoomCalendarInfoForOrderVo.setRoomInfo(roomInfo);
        c2bSelectRoomCalendarInfoForOrderVo.setHotelInfo(hotelInfo);
        c2bSelectRoomCalendarInfoForOrderVo.setRoomCalendarInfos(roomCalendarInfos);
        return c2bSelectRoomCalendarInfoForOrderVo;
    }

}
